# --- 使用最稳定、最通用的官方 Python 镜像 ---
FROM python:3.10-slim

# --- 设置环境变量，避免交互式提示 ---
ENV DEBIAN_FRONTEND=noninteractive

# --- 1. 安装系统级依赖 ---
# 更新 apt 包列表，并安装 Playwright 运行 Chromium 所需的所有系统库
# 这是在干净的 Debian(slim) 系统上运行浏览器所必需的步骤
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    # --- 核心浏览器依赖 ---
    libnss3 \
    libnspr4 \
    libdbus-1-3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libatspi2.0-0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    # --- 字体和渲染依赖 ---
    fonts-liberation \
    libfontconfig1 \
    # --- 清理 apt 缓存 ---
    && rm -rf /var/lib/apt/lists/*

# --- 2. 设置工作目录并安装 Python 依赖 ---
WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# --- 3. 安装 Playwright 浏览器 ---
# 这一步会下载 Chromium 浏览器到容器中
RUN playwright install --with-deps chromium

# --- 4. 复制应用代码 ---
COPY . .

# --- 5. 设置环境变量并暴露端口 ---
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH=/app

# --- 6. 容器启动命令 ---
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
